//
//  MXFindBlockTypeSixTableViewCell.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/14.
//  Copyright (c) 2015年 霄 马. All rights reserved.
//

#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)

#import "MXFindBlockTypeSixTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "MXFindDetailModel.h"
#import "UIImage+WebP.h"
#import "UIImage+MultiFormat.h"
#import "UIImage+GIF.h"
#import "MXFindBlockTypeSixCollectionViewCell.h"
#import "MXNewViewItemDetailVC.h"
#import "MXFindWebViewVC.h"
#import "MXFindItemDetailWebViewVC.h"
@interface MXFindBlockTypeSixTableViewCell ()

@property (nonatomic, retain) NSArray *array;

@property (nonatomic, retain) MXFindBlockTypeSixCollectionViewCell *cell;


@end

@implementation MXFindBlockTypeSixTableViewCell
- (void)dealloc
{
    [_naviSix release];
    [_cell release];
    [_array release];
    [_collectionView release];
    [_mainImageView release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.mainImageView = [[[UIImageView alloc] init] autorelease];
        self.mainImageView.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:self.mainImageView];
        
        self.array = [NSMutableArray array];
        
        [self creatCollectionView];
        
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat WID = self.frame.size.width;
    
    CGFloat mainImageViewX = 0;
    CGFloat mainImageViewY = 0;
    CGFloat mainImageViewW = WID;
    CGFloat mainImageViewH = mainImageViewW;
    self.mainImageView.frame = CGRectMake(mainImageViewX, mainImageViewY, mainImageViewW, mainImageViewH);
    
    
    CGFloat scrollViewX = 0;
    CGFloat scrollViewY = mainImageViewH;
    CGFloat scrollViewW = WID;
    CGFloat scrollViewH = HEIGHTPRO * 80;
    self.collectionView.frame = CGRectMake(scrollViewX, scrollViewY, scrollViewW, scrollViewH);
    self.collectionView.contentSize = CGSizeMake(WIDTHPRO * 80 * self.array.count, 0);

}

- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowLayout.itemSize = CGSizeMake(WIDTHPRO * 80,HEIGHTPRO * 80);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0 , 0 , 0, 0);
    
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,WIDTHPRO * 80 * self.array.count, HEIGHTPRO * 60) collectionViewLayout:flowLayout] autorelease];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.userInteractionEnabled = YES;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    
    
    [self.collectionView registerClass:[MXFindBlockTypeSixCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}
#pragma mark - CollectionView 协议实现部分
/************************* CollectionView Delegate **************************/
//Collection中的组别个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个组别中元件的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}
//重用机制
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXFindBlockTypeSixCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.layer.cornerRadius = 8;
    cell.layer.masksToBounds = YES;
    [cell getInfo:self.model rowNum:indexPath.row];
    return cell;
}



#pragma mark - 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    MXFindItemDetailWebViewVC *ItemVC = [[[MXFindItemDetailWebViewVC alloc] init] autorelease];
    
    NSMutableArray *arr = [NSMutableArray array];
    for (NSDictionary *dic in self.model.product) {
        [arr addObject:[dic objectForKey:@"url"]];
    }
    
    ItemVC.requestString = arr[indexPath.row];
    
    [self.naviSix pushViewController:ItemVC animated:YES];
    
    ItemVC.TitleString = @"商品详情";

    
}


- (void)setModel:(MXFindDetailModel *)model
{
    if (_model != model) {
        _model = model;
    }
    
    self.array = model.product;

//    [self.cell getInfo:model];
    
    NSString *urlString = [model.article stringByReplacingOccurrencesOfString:@"|" withString:@""];
    
    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:urlString]placeholderImage:[UIImage imageNamed:@"plaseholder"]];

        
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
